package com.ody.util.code.generator;

import com.google.common.io.Files;
import com.odianyun.util.io.Closer;
import com.odianyun.util.io.FileUtils;
import com.odianyun.util.io.ZIPUtils;
import org.springframework.util.Assert;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

public class VueFileCodeGenerator extends FileCodeGenerator {
    private String url;
    private boolean unzip;
    
    @Override
    protected void doWriteFile(String content, String targetDir, String file) throws Exception {
        Assert.notNull(url, "Parameter url is required");
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        try {
            conn.setRequestMethod("POST");
            conn.addRequestProperty("content-type", "application/json");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            write(content, conn.getOutputStream());
            conn.connect();
            int code = conn.getResponseCode();
            if (code == 200) {
                System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage());
                InputStream is = conn.getInputStream();
                
                String vueDirPath = FileUtils.appendSeparatorSuffix(FileUtils.getParent(file)) + "vue";
                File vueDir = new File(vueDirPath);
                FileUtils.mkdir(vueDir);
                
                String jsonFile = FileUtils.getFilenameWithoutExtName(file) + ".js";
                Files.write(content.getBytes(Charset.forName("utf-8")), new File(FileUtils.appendSeparatorSuffix(vueDirPath) + jsonFile));
                try {
                    if (unzip) {
                        ZIPUtils.unzip(is, vueDir);
                    } else {
                        OutputStream os = new FileOutputStream(FileUtils.getFilenameWithoutExtName(file) + ".zip");
                        try {
                            FileUtils.pipeStream(is, os);
                        } finally{
                            Closer.close(os);
                        }
                    }
                } finally {
                    Closer.close(is);
                }
            } else {
                System.err.println(conn.getResponseCode() + " " + conn.getResponseMessage());
                FileUtils.pipeStream(conn.getErrorStream(), System.err);
                conn.getErrorStream().close();
            }
        } finally {
            conn.disconnect();
        }
    }
    
    private void write(String content, OutputStream os) throws Exception {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
        bw.write(content);
        bw.close();
    }
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    public boolean isUnzip() {
        return unzip;
    }
    
    public void setUnzip(boolean unzip) {
        this.unzip = unzip;
    }
}
